package com.free.note.controller;

import com.free.common.core.utils.poi.ExcelUtil;
import com.free.common.core.web.controller.BaseController;
import com.free.common.core.web.domain.AjaxResult;
import com.free.common.core.web.domain.R;
import com.free.common.core.web.page.TableDataInfo;
import com.free.common.log.annotation.Log;
import com.free.common.log.enums.BusinessType;
import com.free.common.security.annotation.RequiresPermissions;
import com.free.note.domain.ZoneNote;
import com.free.note.domain.qo.CompareSimilarDegreeQo;
import com.free.note.service.impl.ZoneNoteService;
import com.free.note.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 笔记Controller
 *
 * @author YHL
 * @date 2023-02-26
 */
@RestController
@RequestMapping("/note")
public class ZoneNoteController extends BaseController {
    @Autowired
    private ZoneNoteService zoneNoteService;

    /**
     * 查询笔记列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ZoneNote zoneNote) {
        startPage();
        List<ZoneNote> list = zoneNoteService.selectZoneNoteList(zoneNote);
        return getDataTable(list);
    }
    /**
     * 查询笔记列表
     */
    @PostMapping("/listInner")
    public List<ZoneNote> listInner(@RequestBody ZoneNote zoneNote) {
        startPage();
        return zoneNoteService.selectZoneNoteList(zoneNote);
    }

    /**
     * 导出笔记列表
     */
    @RequiresPermissions("note:note:export")
    @Log(title = "笔记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZoneNote zoneNote) {
        List<ZoneNote> list = zoneNoteService.selectZoneNoteList(zoneNote);
        ExcelUtil<ZoneNote> util = new ExcelUtil<ZoneNote>(ZoneNote.class);
        util.exportExcel(response, list, "笔记数据");
    }

    /**
     * 获取笔记详细信息
     */
    @RequiresPermissions("note:note:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(zoneNoteService.selectZoneNoteById(id));
    }

    /**
     * 新增笔记
     */
    @RequiresPermissions("note:note:add")
    @Log(title = "笔记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZoneNote zoneNote) {
        return success(zoneNoteService.insertZoneNote(zoneNote));
    }

    /**
     * 修改笔记
     */
    @Log(title = "笔记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZoneNote zoneNote) {
        return success(zoneNoteService.updateZoneNote(zoneNote));
    }

    /**
     * 删除笔记
     */
    @RequiresPermissions("note:note:remove")
    @Log(title = "笔记", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(zoneNoteService.deleteZoneNoteByIds(ids));
    }

    @Log(title = "笔记", businessType = BusinessType.DELETE)
    @GetMapping("/randomOne")
    public AjaxResult randomOne(String compilationId) {
        return success(zoneNoteService.randomOne(compilationId));
    }

    @PostMapping("compareSimilarDegree")
    public AjaxResult compareSimilarDegree(@RequestBody CompareSimilarDegreeQo degreeQo) {
        double v = StrUtil.SimilarDegree(degreeQo.getStrA(), degreeQo.getStrB());
        return success(v);
    }

    @GetMapping("notShowAgain/{id}")
    public AjaxResult notShowAgain(@PathVariable("id") Long id) {
        zoneNoteService.notShowAgain(id);
        return success();
    }

    /**
     * 刷新一下题库
     *
     * @param compilationId
     * @return
     */
    @GetMapping("refreshQuantity")
    public AjaxResult refreshQuantity(String compilationId) {
        zoneNoteService.refreshQuantity(compilationId);
        return success();
    }

    @GetMapping("getRemainingQuantity")
    public AjaxResult getRemainingQuantity(String compilationId) {
        return success(zoneNoteService.getRemainingQuantity(compilationId));
    }

    @GetMapping("getNeedRecognitionOne")
    public R<ZoneNote> getNeedRecognitionOne() {
        return R.ok(zoneNoteService.getNeedRecognitionOne());
    }
    @GetMapping("getNeedFileSynchronization")
    public R<ZoneNote> getNeedFileSynchronization() {
        return R.ok(zoneNoteService.getNeedFileSynchronization());
    }

    @PostMapping("/getRandomOneByClassify")
    public R<ZoneNote> getRandomOneByClassify(@RequestBody String classify) {
        ZoneNote zoneNote = zoneNoteService.getRandomOneByClassify(classify);
        return R.ok(zoneNote);
    }

    /**
     * 轮播图
     *
     * @return
     */
    @GetMapping("/getSwiperList")
    public R<List<String>> getSwiperList() {
        List<String> zoneNotes = zoneNoteService.getSwiperList();
        return R.ok(zoneNotes);
    }

}
